﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Text;
public partial class Bidder_TraSupervise_xieyi : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    private string _accountid_ps_M = "0", _accoountname = "", _strProjectId="";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            _accountid_ps_M = _Account.AccountId.ToString();
            _accoountname = _Account.DWName;
            _strProjectId = SysFun.IsSQL(SysFun.ToName(Request.QueryString["Projectid"].ToString()));
            if (!IsPostBack)
            {
                selectArea();//绑定地区
            }

            string _psr = DropDownList1.SelectedItem == null ? "" : DropDownList1.SelectedItem.Text;
            string _psrid = DropDownList1.SelectedItem == null ? "" : DropDownList1.SelectedItem.Value;
            selectPSF();//绑定相应地区的配送人
            if(DropDownList1.Items.Contains(new ListItem(_psr,_psrid)))
            {
                DropDownList1.Items.FindByValue(_psrid).Selected = true ;
            }
            showGridView();
        }

    }


    public void showGridView()
    {
       
        switch (_strProjectId)
        {
            case "8":
                //fieldName = "最高零售价";
                //field = " ,convert( numeric(20,2), RetailPrice)  as " + fieldName;
                GridView2.Columns[10].HeaderText = "挂网价";
                GridView2.Columns[11].Visible = false;
                break;
            case "9":
                GridView2.Columns[10].HeaderText = "最高零售价";
                GridView2.Columns[11].Visible = false;
                break;
            case "10":
                //priceTitle = "最高限价";
                //fieldName = "最高零售价";
                //field = " ,convert( numeric(20,2), RetailPrice)  as " + fieldName;
                GridView2.Columns[10].HeaderText = "最高限价";
                GridView2.Columns[11].HeaderText = "最高零售价";
                break;
            case "11":
                GridView2.Columns[10].HeaderText = "挂网价";
                GridView2.Columns[11].Visible = false;
                break;
            case "12":
                GridView2.Columns[10].HeaderText = "最高零售价";
                GridView2.Columns[11].Visible = false;
                break;

        }
        string _strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods INNER JOIN SupplyPlan_GoodsNext ON SupplyPlan_Goods.SupplyGoodsId = SupplyPlan_GoodsNext.SupplyGoodsId AND SupplyPlan_Goods.AccountId_PS = SupplyPlan_GoodsNext.Accountid_ps_M";                                               //要查询的表明
        dbPage.Key = "SupplyPlan_Goodsnext.[id]";                                               //表的主键    
        dbPage.Where = fctConditions();                                           //查询条件    
        dbPage.SelectField = "id,GoodsId,ProductName,MedicineModel,Outlookc,Factor,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,UserCode_TB,CompanyName_TB,CompanyName_PS,areaname,companyname_ps_B1 ,UserCode_ps,convert( numeric(20,2), bidprice) as bidprice,convert( numeric(20,2), RetailPrice) as RetailPrice";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   

        if (Request.Form["PrintPrice"] == "打印")
        {
               dbPage.PageSize = 1000;                                              //没有显示记录条数   
        }
        else
        {
            dbPage.PageSize = 10;                                                    //没有显示记录条数   
        }
        dbPage.OrderBy = "CompanyName_TB,productname,medicinemodel";//排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView2.DataSource = dr;
        GridView2.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip2.DbPage = dbPage;//将数据传给控键 

    }
    /// <summary>
    /// 拼接查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string _strWhere = " Accountid_ps_M=" + _accountid_ps_M + " and SupplyPlan_Goods.isuse='1' and projectid="+_strProjectId+" and";
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
          
            //药品通用名
            string _strProductName = SysFun.ToTrim(ProductName.Text);
            if (_strProductName != "")
            {
                _strWhere += " ProductName like '%" + _strProductName + "%' And";
            }
            //药品规格
            string _strOutlookc = SysFun.ToTrim(Outlookc.Text);
            if (_strOutlookc != "")
            {
                _strWhere += " Outlookc like '%" + _strOutlookc + "%' And";
            }
            string areanameselect = lstAreaName.SelectedValue;
            if (areanameselect != "")
            {
                    _strWhere += " areaid = '" + areanameselect + "' and";

            }
            //分配送企业
            string DropDownList1select = DropDownList1.SelectedValue;
            if (DropDownList1select != "")
            {     
                if (DropDownList1select == "0") { }
                else
                {
                _strWhere += " Accountid_ps_B1 = '" + DropDownList1select + "' and";
                }
            }
            //药品流水号
            string _strGoodsid = txtGoodsId.Text.Trim();
            if (_strGoodsid != "")
            {
                _strWhere += " Goodsid ='" + _strGoodsid + "' And";
            }

            if (_strWhere != "")
            {
                _strWhere = "  " + _strWhere.Substring(0, _strWhere.Length - 3);
            }
        }
        else
        {
            _strWhere = "";

        }
        return _strWhere;
    }
    private void selectArea()//绑定地区
    {
        string strSql = "select * from area  where areaid >530000 and areaid<540000 and areaid%100=0 ";
        Db DbProject = new Db();

        System.Data.IDataReader dr = DbProject.DataReader(strSql);
        lstAreaName.DataSource = dr;
        lstAreaName.DataTextField = "areaname";
        lstAreaName.DataValueField = "areaid";
        lstAreaName.DataBind();
        dr.Close();
        dr.Dispose();
        DbProject.Close();
    }
    private void selectPSF()//绑定相应地区的配送人
    {

        string strSql = "";
        //地区查询
        string areanameselect = lstAreaName.SelectedValue;
        strSql = "select distinct Accountid_ps_B1,companyname_ps_B1 from dbo.SupplyPlan_GoodsNext where Accountid_ps_M=" + _accountid_ps_M + " and areaid=" + areanameselect;
        Db DbProject = new Db();
        DataTable dt = DbProject.DataTable(strSql);
        DataRow dw = dt.NewRow();
        dw[0] = "0";
        dw[1] = "全部";
        dt.Rows.InsertAt(dw,0);
      
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "companyname_ps_B1";
        DropDownList1.DataValueField = "Accountid_ps_B1";
        DropDownList1.DataBind();
        dt.Clear();
        DbProject.Close();
    }

  
    #region TraDes 成员

    public string Name
    {
        get { return "打印配送协议"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "打印配送协议"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
            return _AccountType;
        }
    }

    #endregion

    protected void lstAreaName_SelectedIndexChanged(object sender, EventArgs e)
    {
       selectPSF();//绑定地区
    }

    protected void PrintPrice_ServerClick(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "0")
        {
            Response.Write("<script>alert('请选择具体二级配送企业！');</script>");
        }
        else
        {
            if (GridView2.Rows.Count == 0)
            {
                Response.Write("<script>alert('请选择正确的地区和二级配送企业！');</script>");
                return;
            }
            printWord();
        }
    }
    //打印word
    private void printWord()
    { 
    
        Response.ContentType = "application/ms-word";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中标药品配送协议书" + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".doc\"");
        StringBuilder sb = new StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        sb.Append("<html><body margin-top='10%' margin-bottom='10%' margin-left='15%' margin-right='15%'>");
      
        sb.Append("<center><h3>2007年度云南省药品集中招标采购中标药品配送协议书</h3></center></p>");
        sb.Append("</br>");
        sb.Append("</br>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;甲方（中标人或持有中标人授权声明者）：" + _accoountname + "</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;乙方（被授权配送单位）：" + DropDownList1.SelectedItem.Text + "</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;鉴于甲方作为“2007年度云南省医疗机构药品集中招标采购”的中标人，履约期间应严格按照招标文件和甲方的要求，提供优质、全面、快捷的配送服务，对所有中标商品的质量负责。</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;按照招标文件第二章第28款第二条的约定：“中标人在各省辖市可委托原则上不超过四家的经销商进行转配送（中标人若为生产企业的可由其经销公司在各省辖市委托原则上不超过四家的经销商进行转配送或指定一家一级配送企业配送，一级配送企业可在各省辖市委托原则上不超过四家的经销商进行转配送）”，甲方就以下中标药品授权乙方作为二级配送企业，负责为配送区域<u><b>"+lstAreaName.SelectedItem.Text+"</b></u>（省辖市名称）医疗机构提供配送服务，有效期为本招标采购周期内，详细药品目录见附表。</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;本协议书双方协议如下：</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;1、	甲方将以上中标药品的配送资格授权给乙方,同时保留甲方在该区域的配送权；</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;2、	乙方享有该药品配送方面的权力与义务，如乙方不能履行配送职责，甲方需调整被授权配送单位，以满足需求；</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;3、	由于甲方是中标人，并于投标时提交了响应性承诺文件，根据招标文件相关规定，履约期间甲方仍将承担中标人所应有的一切责任与义务；</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;4、	甲方若非中标人，应持有中标人授权声明并履行中标人的权利和义务；</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;5、	该协议书一式叁份，甲乙双方各留存一份，云南省医药招标采购管理服务中心存档一份。</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp; 协议双方可根据需要增加相应条款,本协议书自盖章之日起生效。</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp; 甲方（公章）：  &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;乙方（盖章）：</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp; 法人代表签字：  &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;法人代表签字：</p>");
        sb.Append("<p >&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 年&nbsp;&nbsp;&nbsp;&nbsp;月&nbsp;&nbsp;&nbsp;&nbsp;日</p>");
        sb.Append("<p>附表:</p>");
        sb.Append("<center>");
        this.GridView2.RenderControl(hw);
        sb.Append("</center>");
        sb.Append("<p>以上中标品目共计<b>" + GridView2.Rows.Count + "</b>个（附表需加盖公章）。<p>");
        sb.Append("</div>");
        sb.Append("</body></html>");
        Response.Write(sb.ToString());
        Response.End(); 
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    } 
}
